package org.csu.onlineedubackend.service.impl;

import com.baidu.aip.face.AipFace;
import org.csu.onlineedubackend.service.FaceService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;

@Service
public class FaceServiceImpl implements FaceService {

    @Value("${baidu.aip.app-id}")
    private String APP_ID;
    @Value("${baidu.aip.api-key}")
    private String API_KEY;
    @Value("${baidu.aip.secret-key}")
    private String SECRET_KEY;

    private AipFace client;

    @PostConstruct
    public void init() {
        client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(10000);
        client.setSocketTimeoutInMillis(60000);
    }

    @Override
    public JSONObject register(String image, String imageType, String userId, String groupId) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<>();
        options.put("user_info", "user's info"); // 用户资料，可选
        options.put("quality_control", "LOW"); // 图片质量控制
        options.put("liveness_control", "NONE"); // 活体检测控制

        // 人脸注册
        return client.addUser(image, imageType, groupId, userId, options);
    }

    @Override
    public JSONObject search(String image, String imageType, String groupId) {
        HashMap<String, Object> options = new HashMap<>();
        options.put("max_user_num", "1"); // 最多返回1个匹配用户
        options.put("match_threshold", 80); // 匹配阈值，超过80分认为匹配
        options.put("quality_control", "LOW");
        options.put("liveness_control", "NONE");

        // 人脸搜索
        return client.search(image, imageType, groupId, options);
    }
} 